<template>
  <div class="main">
    <el-card class="page-container">
      <template #header>
        <div class="page-header">
          <span>重置密码</span>
        </div>
      </template>
      <el-row class="page-content">
        <el-col :span="12">
          <el-form
            :model="{ oldPassword, newPassword, rePassword }"
            :rules="rules"
            label-width="300px"
            size="large"
            ref="formRef"
          >
            <el-form-item label="旧密码" prop="oldPassword">
              <el-input v-model="oldPassword" type="password"></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="newPassword">
              <el-input v-model="newPassword" type="password"></el-input>
            </el-form-item>
            <el-form-item label="确认新密码" prop="rePassword">
              <el-input v-model="rePassword" type="password"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="resetPassword"
                >提交修改</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>
<script setup>
import useUserInfoStore from "@/stores/userInfo.js";
import { ref } from "vue";

// 获取用户信息
const userInfoStore = useUserInfoStore();
import { ElMessage, ElMessageBox } from "element-plus";

// 定义用户信息
const userInfo = ref({ ...userInfoStore.info });
// 定义旧密码、新密码和确认新密码
const oldPassword = ref("");
const newPassword = ref("");
const rePassword = ref("");
// 表单引用
const formRef = ref();
// 表单校验
const rules = {
  oldPassword: [
    { required: true, message: "请输入旧密码", trigger: "blur" },
    {
      pattern: /^\S{6,20}$/,
      message: "密码必须是6-20位的非空字符串",
      trigger: "blur",
    },
  ],
  newPassword: [
    { required: true, message: "请输入新密码", trigger: "blur" },
    {
      pattern: /^\S{6,20}$/,
      message: "密码必须是6-20位的非空字符串",
      trigger: "blur",
    },
  ],
  rePassword: [
    { required: true, message: "请确认新密码", trigger: "blur" },
    {
      validator: (rule, value, callback) => {
        if (value !== newPassword.value) {
          callback(new Error("两次输入的密码不一致"));
        } else {
          callback();
        }
      },
      trigger: "blur",
    },
  ],
};
import { updatePasswordService } from "@/api/user.js";
// 更新用户密码
const resetPassword = async () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      // 调用后台接口更新用户密码
      let result = await updatePasswordService({
        old_pwd: oldPassword.value,
        new_pwd: newPassword.value,
        re_pwd: rePassword.value,
      });
      ElMessage.success("密码修改成功");
    }
  });
};
</script>

<style lang="scss" scoped>
.main {
  background: url("@/assets/starfield.jpg") no-repeat center center / cover;
  height: 100%;
  width: 100%;
  display: flex;
  justify-content: center;
  .page-container {
    width: 70%;
    margin-top: 1%;
    .page-content {
      margin-top: 5%;
    }
  }
}
</style>